package com.wwk.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.wwk.constant.MessageConstant;
import com.wwk.constant.RedisConstant;
import com.wwk.entity.PageResult;
import com.wwk.entity.QueryPageBean;
import com.wwk.entity.Result;
import com.wwk.pojo.Setmeal;
import com.wwk.service.CheckMealService;
import com.wwk.utils.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.JedisPool;

import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * @Classname CheckMealController
 * @Description TODO
 * @Date 2020/11/1 20:54
 * @Created by com.wwk
 * @Author com.wwk
 */
@RestController
@RequestMapping("/checkmeal")
public class CheckMealController {

    @Reference
    private CheckMealService mealService;

    @Autowired
    private JedisPool jedisPool;

    @RequestMapping("/add")
    public Result add(@RequestBody Setmeal setmeal, Integer[] checkgroupIds){
        try{
            mealService.add(setmeal,checkgroupIds);
            return new Result(true, MessageConstant.ADD_SETMEAL_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_SETMEAL_FAIL);
        }
    }

    @RequestMapping("/upload")
    public Result upload(@RequestParam("imgFile") MultipartFile imgFile){
        try{
            final String originalFilename = imgFile.getOriginalFilename();
            int lastIndexOf = originalFilename.lastIndexOf(".");
            //获取文件后缀
            String suffix = originalFilename.substring(lastIndexOf); //.jpg
            //使用UUID随机产生文件名称，防止同名文件覆盖
            String fileName = UUID.randomUUID().toString() + suffix;
            QiniuUtils.upload2Qiniu(imgFile.getBytes(),fileName);
            //将上传图片名称存入Redis，基于Redis的Set集合存储
            jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_RESOURCES,fileName);
            jedisPool.close();
            //图片上传成功
            return new Result(true,MessageConstant.PIC_UPLOAD_SUCCESS,fileName);

        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("图片上传失败！");
            //图片上传失败
            return new Result(false,MessageConstant.PIC_UPLOAD_FAIL);
        }
    }

    @RequestMapping("/deleteById/{mealId}")
    public Result deleteById(@PathVariable(value = "mealId") Integer mealId){
        try{
            mealService.delete(mealId);
            return new Result(true,"删除套餐成功！");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(true,"删除套餐失败！");
        }

    }

    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        System.out.println(queryPageBean);
        //需要查询总记录数和分页数据
        try{
            return mealService.findPage(queryPageBean);
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }
}
